package com.rqf.mr.service.impl;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import oracle.jdbc.OracleTypes;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.rqf.mr.service.IDrugService;
import com.rqf.mr.service.IMessageService;

@Service("MessageService")
public class MessageService implements IMessageService {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private IDrugService drugService;

	@Override
	public List<Map<String, Object>> getByNurseId(String loginName) {
		if ( StringUtils.isEmpty(loginName) ) {
			return null;
		}
		List<Map<String, Object>> viewDrugList = drugService.getDrugInfoByNurseId(loginName);
		String sql = "select * from drugplan dd where dd.NURSEEMPID='" + loginName + "'";
		List<Map<String, Object>> tableDrugList = jdbcTemplate.queryForList(sql);
		for (int i = 0; i < viewDrugList.size(); i++) {
			String viewOrderId = viewDrugList.get(i).get("ORDID").toString();
			for (int j = 0; j < tableDrugList.size(); j++) {
				String tableOrderId = tableDrugList.get(j).get("ORDID").toString();
				if ( viewOrderId.equals(tableOrderId) ) {
					viewDrugList.remove(i);
					tableDrugList.remove(j);
					i--;
					break;
				}
			}
			/*字符串的处理*/
			String patiName = viewDrugList.get(i).get("PATINAME").toString();
			String ordDes = viewDrugList.get(i).get("ORDDES").toString();
			String comment = "";
			String []ord=ordDes.split(",",1);
			comment += patiName+ord[0]+"]";
			viewDrugList.get(i).put("comment", comment);
		}
		return viewDrugList;
	}
	
	@Override
	public List<Map<String, Object>> getPatiNameByNurseId(String loginName) {
		if ( StringUtils.isEmpty(loginName) ) {
			return null;
		}
		String viewSql = "select DISTINCT PATINAME,INPATIID from view_ehis_pqzx "
				+ "where NURSEEMPID LIKE '%" + loginName + "%'";
		List<Map<String, Object>> viewDrugList = jdbcTemplate.queryForList(viewSql);
		return viewDrugList;
	}

	@Override
	public List<Map<String, Object>> getNotReadList(String loginName) {
		if ( StringUtils.isEmpty(loginName) ) {
			return null;
		}
		String sql = "Select  * from message where SENDTO='"
				+ loginName + "' AND ISOK = '1'";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		/* 此处应该 更新isOk*/
		return list;
	}

	@Override
	public Boolean messageAdd(String ordId) {
		if ( StringUtils.isEmpty(ordId) ) {
			return null;
		}
		int result =0;
		DataSource dataSource = jdbcTemplate.getDataSource();  
		Connection conn=null;  
		try {
			conn=dataSource.getConnection();
			CallableStatement cs = conn.prepareCall("{call MESSAGE_ADD(?,?)}");   
			cs.setString(1, ordId);  
			cs.registerOutParameter(2, OracleTypes.NUMBER);
			cs.execute();  
			result = cs.getInt(2);//获取单个返回值
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}  
		if ( result>0 ) {
			return true;
		}else {
			return false;
		}
	}

}
